package com.larry.tinyweb.util;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by larry on 16/8/14.
 */
public class Page {
    private Integer pageSize;
    private Integer startIndex;
    private Integer pageCount;
    private Integer currentPage;
    private List list;

    public Page(Integer pageSize, Integer currentPage) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.startIndex = pageSize*(currentPage-1);
    }

    public static Page getPage(HttpServletRequest req) {
        String pageNumStr = req.getParameter("pageNum");
        Integer pageNum = pageNumStr==null? 1:Integer.parseInt(pageNumStr);
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = pageSizeStr==null? 15:Integer.parseInt(pageSizeStr);
        if (pageNum == null || pageSize == null) return null;
        else return new Page(pageSize, pageNum);
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getNextPage() {
        if (currentPage >= pageCount) return pageCount;
        else return currentPage + 1;
    }

    public Integer getPrePage() {
        if (currentPage <= 1) return 1;
        else return currentPage - 1;
    }
}
